package com.dataaccess.dataaccess.web.web;

import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseController;
import com.dataaccess.dataaccess.common.utils.appkit.FirefightingiotKit;
import com.dataaccess.dataaccess.config.rocketmq.UserProducer;
import com.dataaccess.dataaccess.model.AlarmAllDeviceDataChanged;
import com.dataaccess.dataaccess.support.annotaion.Action;
import com.dataaccess.dataaccess.support.annotaion.At;
import com.dataaccess.dataaccess.support.model.RespEntity;
import com.dataaccess.dataaccess.support.utils.MyLogger;
import com.dataaccess.dataaccess.web.mapper.AlarmAllDeviceDataChangedMapper;
import com.dataaccess.dataaccess.web.service.DeviceAboatCombustiblegasService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 可燃气体设备控制器
 */
@Action
@RequestMapping("combustiblegas")
public class DeviceAboatCombustiblegasController extends BaseController {

    @Resource
    public MyLogger logger;
    @Resource
    public DeviceAboatCombustiblegasService deviceAboatCombustiblegasService;
    @Resource
    private AlarmAllDeviceDataChangedMapper alarmAllDeviceDataChangedMapper;
    @Resource
    UserProducer userProducer;

    /**
     * 订阅--调用相关接口
     * @param
     * @return
     */
    @At(value = "device/getInfo", method = RequestMethod.POST)
    public RespEntity getInfo(@RequestBody(required = false) JSONObject jsonParam) throws Exception {
        return deviceAboatCombustiblegasService.getInfo(jsonParam);
    }

    /**
     * 回调地址--设备信息变化，订阅后推送设备信息变化通知
     * @param
     * @return
     */
    @At(value = "device/deviceInfoChanged", method = RequestMethod.POST)
    public RespEntity deviceInfoChanged(@RequestBody(required = false) JSONObject jsonParam) {
        return deviceAboatCombustiblegasService.deviceInfoChanged(jsonParam);
    }

    /**
     * 回调地址：设备数据变化，订阅后推送设备数据变化通知
     * @param
     * @return
     */
    @At(value = "device/deviceDataChanged", method = RequestMethod.POST)
    public RespEntity deviceDataChanged(@RequestBody(required = false) JSONObject jsonParam) {
        logger.info("gasdeviceDataChanged"+"------"+jsonParam);
        //报警数据
        AlarmAllDeviceDataChanged alarmAllDeviceDataChanged = new AlarmAllDeviceDataChanged();
        alarmAllDeviceDataChanged.setId(UUID.randomUUID().toString());
        alarmAllDeviceDataChanged.setJsondata(jsonParam.toString());
        alarmAllDeviceDataChanged.setType("4");
        alarmAllDeviceDataChangedMapper.insert(alarmAllDeviceDataChanged);
        jsonParam.put("deviceFlag","combustiblegas");
        String mqFlag = FirefightingiotKit.getStr("mqFlag","mq");
        if(mqFlag.equals("1")){
            return deviceAboatCombustiblegasService.deviceDataChanged(jsonParam);
        }else if(mqFlag.equals("3")){
            userProducer.produder(jsonParam.toString());
        }
        return RespEntity.success();
    }



    /**
     * test mq
     * @param
     * @return
     */
    @At(value = "testmq", method = RequestMethod.POST)
    public RespEntity testmq(@RequestBody(required = false) JSONObject jsonParam) {
        return RespEntity.success();
    }
   
}